package com.lyqg.action;

import static com.lyqg.Constants.SESSION_USER;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.util.WebUtils;

import com.lyqg.domain.User;

public class LoginFilter implements Filter {

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		String req_uri = ((HttpServletRequest) request).getRequestURL()
				.toString();
		if (req_uri.indexOf("/user/") != -1) {
			User user = (User) WebUtils.getSessionAttribute(
					(HttpServletRequest) request, SESSION_USER);
			if (user == null) {
				((HttpServletResponse) response).sendRedirect("/");
				return;
			}
		}
		chain.doFilter(request, response);
		return;
	}

	@Override
	public void destroy() {

	}

}
